/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package intelligenttm.ITMCarto;

import intelligenttm.InPutDlg.AddNewTypeDlgController;
import intelligenttm.SourceFile.CartoType;
import intelligenttm.SourceFile.MyColorChooser;
//import java.awt.Color;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.SwingNode;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javax.swing.JColorChooser;

/**
 * FXML Controller class
 *
 * @author 望舒凌霄
 */
public class S3SelectionTypeController implements Initializable {
    @FXML
    private AnchorPane RootPane;
    @FXML
    private Button BtnCancel;
    @FXML
    private Button BtnLast;
    @FXML
    private Button BtnNext;
    @FXML
    private TableView<CartoType> TypeTableView;
    @FXML
    private TableColumn<CartoType, String> TypeInt;
    @FXML
    private TableColumn<CartoType, String> Type;
    @FXML
    private TableColumn<CartoType, ImageView> TypeSample;
    @FXML
    private Button BtnDeleteType;
    @FXML
    private Button BtnAddType;
    @FXML
    private Button BtnChooseColor;
    @FXML
    private Rectangle ColorRect;
    
    private List<CartoType> CartoTypeList=new ArrayList<>();
    private ObservableList<CartoType> CartoTypeItemList =  FXCollections.observableArrayList();
    private Color BaseColor=Color.rgb(200,200,200);
    private int WhichBtn=0;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        CartoTypeList.clear();
        CartoTypeItemList.clear();
        SetTableColumn();
        // TODO
    }    
    public void SetCartoTypeList(List<CartoType> tCartoTypeList ){
        CartoTypeList=tCartoTypeList;
        CartoTypeItemList.clear();
        CartoTypeItemList.addAll(CartoTypeList);
        TypeTableView.setItems(CartoTypeItemList);
    }
    
    @FXML
    private void HandleBtnCancel(ActionEvent event) {
        WhichBtn=0;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnLast(ActionEvent event) {
        WhichBtn=-1;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnNext(ActionEvent event) {
        CartoTypeList.clear();
        CartoTypeItemList.stream().forEach((CartoTypeItemList1) -> {
            CartoTypeList.add(CartoTypeItemList1);
        });
        WhichBtn=1;
        RootPane.getScene().getWindow().hide();
    }

    @FXML
    private void HandleBtnDeleteType(ActionEvent event) {
        int i=TypeTableView.getSelectionModel().getFocusedIndex();
        if(i!=-1&&i>=0){
            CartoTypeItemList.remove(i);
        }
    }

    @FXML
    private void HandleBtnAddType(ActionEvent event) throws IOException {
        //System.out.println(System.getProperty("user.dir"));
        FXMLLoader fXMLLoader2 = new FXMLLoader(getClass().getResource("/intelligenttm/InPutDlg/AddNewTypeDlg.fxml"));
        Parent root2=fXMLLoader2.load();
        
        AddNewTypeDlgController MyController1=fXMLLoader2.getController();
        Scene scene2 = new Scene(root2);
        Stage stage2 = new Stage();
        stage2.setTitle("添加新类型");
        stage2.setScene(scene2);
        stage2.showAndWait();
        CartoType CT=MyController1.getCPType();
        CT.setNumber(""+(CartoTypeItemList.size()+1));
        CartoTypeItemList.add(CT);
        TypeTableView.setItems(CartoTypeItemList);
    }

    @FXML
    private void HandleBtnChooseColor(ActionEvent event) {
        MyColorChooser MCC=new MyColorChooser();
        if(!MCC.isNull()){
            BaseColor=Color.rgb(MCC.GetR(),MCC.GetG(),MCC.GetB(),MCC.GetA()/255);
            ColorRect.setFill(BaseColor);
        }
    }
    
    private void SetTableColumn(){
        TypeInt.setCellValueFactory(new PropertyValueFactory<>("Number"));
        Type.setCellValueFactory(new PropertyValueFactory<>("TypeName"));
        TypeSample.setCellValueFactory(new PropertyValueFactory<>("TypeSampleIMG"));
    }

    public List<CartoType> getCartoTypeList() {
        return CartoTypeList;
    }

    public int getWhichBtn() {
        return WhichBtn;
    }

    public Color getBaseColor() {
        return BaseColor;
    }
    
}
